/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.sdRankingSys.Listeners;

import com.SySammy.sdRankingSys.Handlers.VocationHandler;
import com.SySammy.sdRankingSys.prop.sdRankProperties;
import com.SySammy.sdRankingSys.sdRS;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeSet;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Skeleton;
import org.bukkit.entity.Spider;
import org.bukkit.entity.Zombie;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityListener;

public class sdEntityListener extends EntityListener {

    sdRS plugin;

    public sdEntityListener(sdRS plugin) {
        this.plugin = plugin;
    }
    HashMap<Integer, Player> DeadList = new HashMap<Integer, Player>();

    @Override
    public void onEntityDamage(EntityDamageEvent event) {
        if ((event instanceof EntityDamageByEntityEvent && event.getEntity() instanceof LivingEntity && ((EntityDamageByEntityEvent) event).getDamager() instanceof Player)) {
            if (event instanceof EntityDamageByEntityEvent && event.getEntity() instanceof Zombie) {
                Player damager = (Player) ((EntityDamageByEntityEvent) event).getDamager();
                LivingEntity le = (LivingEntity) ((EntityDamageByEntityEvent) event).getEntity();
                if (le.getHealth() <= event.getDamage()) {
                    TreeSet<Player> dead = new TreeSet<Player>();
                    dead.add(damager);
                    DeadList.put(event.getEntity().getEntityId(), damager);
                }
                return;
            } else if (event instanceof EntityDamageByEntityEvent && event.getEntity() instanceof Skeleton) {
                Player damager = (Player) ((EntityDamageByEntityEvent) event).getDamager();
                LivingEntity le = (LivingEntity) ((EntityDamageByEntityEvent) event).getEntity();
                if (le.getHealth() <= event.getDamage()) {
                    if (le.getHealth() <= event.getDamage()) {
                        DeadList.put(event.getEntity().getEntityId(), damager);
                    }
                    return;
                }
            } else if (event instanceof EntityDamageByEntityEvent && event.getEntity() instanceof Spider) {
                Player damager = (Player) ((EntityDamageByEntityEvent) event).getDamager();
                LivingEntity le = (LivingEntity) ((EntityDamageByEntityEvent) event).getEntity();
                if (le.getHealth() <= event.getDamage()) {
                    if (le.getHealth() <= event.getDamage()) {
                        DeadList.put(event.getEntity().getEntityId(), damager);
                    }
                    return;
                }
            } else if (event instanceof EntityDamageByEntityEvent && event.getEntity() instanceof Creeper) {
                Player damager = (Player) ((EntityDamageByEntityEvent) event).getDamager();
                LivingEntity le = (LivingEntity) ((EntityDamageByEntityEvent) event).getEntity();
                if (le.getHealth() <= event.getDamage()) {
                    if (le.getHealth() <= event.getDamage()) {
                        DeadList.put(event.getEntity().getEntityId(), damager);
                    }
                    return;
                }
            }
        }
    }

    @Override
    public void onEntityDeath(EntityDeathEvent event) {
        if (!DeadList.containsKey(event.getEntity().getEntityId())) {
            return;
        }
        VocationHandler VH = new VocationHandler(plugin);
        if (event.getEntity() instanceof Zombie) {
            Player damager = (Player) DeadList.get(event.getEntity().getEntityId());
            VH.AddExp(damager, (sdRankProperties.ZombieExp));
            DeadList.remove(event.getEntity().getEntityId());
            return;
        } else if (event.getEntity() instanceof Skeleton) {
            Player damager = (Player) DeadList.get(event.getEntity().getEntityId());
            VH.AddExp(damager, (sdRankProperties.SkeletonExp));
            DeadList.remove(event.getEntity().getEntityId());
            return;
        } else if (event.getEntity() instanceof Spider) {
            Player damager = (Player) DeadList.get(event.getEntity().getEntityId());
            VH.AddExp(damager, (sdRankProperties.SpiderExp));
            DeadList.remove(event.getEntity().getEntityId());
            return;
        } else if (event.getEntity() instanceof Creeper) {
            Player damager = (Player) DeadList.get(event.getEntity().getEntityId());
            VH.AddExp(damager, (sdRankProperties.CreeperExp));
            DeadList.remove(event.getEntity().getEntityId());
            return;
        }
    }
}
